/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vastcm.stuhealth.client;

import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jidesoft.plaf.LookAndFeelFactory;
import com.vastcm.stuhealth.client.entity.CustomSetting;
import com.vastcm.stuhealth.client.entity.service.ICustomSettingService;
import com.vastcm.stuhealth.client.entity.service.core.ICoreService;
import com.vastcm.stuhealth.client.framework.ui.EditUI;
import com.vastcm.stuhealth.client.framework.ui.UIFrameworkUtils;
import com.vastcm.stuhealth.client.framework.ui.UIManagerHelper;
import com.vastcm.stuhealth.client.utils.ExceptionUtils;

/**
 * 
 * @author house
 */
public class CustomSettingPanel extends EditUI<CustomSetting> {

	private Logger logger = LoggerFactory.getLogger(CustomSettingPanel.class);
	private Map<String, String> lookAndFeelMap = new HashMap<String, String>();
	private File pfile = new File(System.getProperty("appHome") + File.separator + "config/custom.properties");

	/**
	 * Creates new form CustomSettingPanel
	 */
	public CustomSettingPanel() {
		initComponents();
	}

	@Override
	public void onLoad() throws Exception {
		UIManager.LookAndFeelInfo[] laf = UIManagerHelper.getInstalledLookAndFeels();
		for (UIManager.LookAndFeelInfo l : laf) {
			logger.info("LookAndFeel: " + l.getName() + "=" + l.getClassName());
			lookAndFeelMap.put(l.getName(), l.getClassName());
			cbLookAndFeel.addItem(l.getName());
		}
		super.onLoad();
	}

	@Override
	public void loadData() throws IOException {
		//        List<CustomSetting> ls = getService().getAll();
		//        if(ls != null && !ls.isEmpty()) {
		//            data = ls.get(0);
		//            cbLookAndFeel.setSelectedItem(data.getLookAndFeelName());
		//        }
		if (!pfile.exists()) {
			pfile.createNewFile();
		}
		Properties prop = new Properties();
		prop.load(new FileInputStream(pfile));
		if (prop.getProperty("lookAndFeel") == null) {
			prop.setProperty("lookAndFeel", "Nimbus");
			cbLookAndFeel.setSelectedItem("Nimbus");
		} else {
			cbLookAndFeel.setSelectedItem(prop.getProperty("lookAndFeel"));
		}
	}

	@Override
	public void storeData() throws FileNotFoundException, IOException {
		String lafName = (String) cbLookAndFeel.getSelectedItem();
		//        data.setLookAndFeelName(lafName);
		//        data.setLookAndFeelClassName(lookAndFeelMap.get(lafName).getClassName());
		Properties prop = new Properties();
		prop.setProperty("lookAndFeel", lafName);
		prop.store(new FileOutputStream(pfile), "CREATE BY VASTCM");
		logger.info("properites file save to: " + pfile.getAbsolutePath());
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jToolBar1 = new javax.swing.JToolBar();
		jToolBar1.setFloatable(false);
		btnSave = new javax.swing.JButton();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		cbLookAndFeel = new javax.swing.JComboBox();

		setPreferredSize(new Dimension(300, 150));
		setLayout(new java.awt.BorderLayout());

		jToolBar1.setRollover(true);

		btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/system_save.gif"))); // NOI18N
		btnSave.setText("保存");
		btnSave.setFocusable(false);
		btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		btnSave.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSaveActionPerformed(evt);
			}
		});
		jToolBar1.add(btnSave);

		add(jToolBar1, java.awt.BorderLayout.PAGE_START);

		jPanel1.setLayout(null);

		jLabel1.setText("界面风格");
		jPanel1.add(jLabel1);
		jLabel1.setBounds(10, 10, 80, 25);

		jPanel1.add(cbLookAndFeel);
		cbLookAndFeel.setBounds(80, 10, 200, 25);

		add(jPanel1, java.awt.BorderLayout.CENTER);
	}// </editor-fold>//GEN-END:initComponents

	private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
		try {
			storeData();
			String lafName = (String) cbLookAndFeel.getSelectedItem();
			UIManagerHelper.setLookAndFeel(UIManagerHelper.getLafClassName(lafName));

			//			for(Window window : JFrame.getWindows()) {
			//		        SwingUtilities.updateComponentTreeUI(window);
			//		    }
			//com.jidesoft.action.CommandBarFactory.a(Container paramContainer)
			if (!(LookAndFeelFactory.isCurrentLnfDecorated())) {
				UIFrameworkUtils.getMainUI().dispose();
				UIFrameworkUtils.getMainUI().setUndecorated(true);
				UIFrameworkUtils.getMainUI().getRootPane().setWindowDecorationStyle(1);
			}

			if (UIFrameworkUtils.getMainUI().isUndecorated()) {
				if (LookAndFeelFactory.isCurrentLnfDecorated()) {
					UIFrameworkUtils.getMainUI().dispose();
					UIFrameworkUtils.getMainUI().setUndecorated(false);
					UIFrameworkUtils.getMainUI().getRootPane().setWindowDecorationStyle(0);
				}
			}
			//			JOptionPane.showMessageDialog(this, "更改界面风格后，需要重新启动本系统才生效！");
		} catch (Exception ex) {
			ExceptionUtils.writeExceptionLog(logger, ex);
			JOptionPane.showMessageDialog(this, "保存失败！详见日志。");
		}
	}//GEN-LAST:event_btnSaveActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton btnSave;
	private javax.swing.JComboBox cbLookAndFeel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JToolBar jToolBar1;

	// End of variables declaration//GEN-END:variables

	@Override
	public ICoreService<CustomSetting> getService() {
		return AppContext.getBean("customSettingService", ICustomSettingService.class);
	}
}
